package com.burgeon.backend.SHOP.rest;
/**
 * 积分商城创建零售单
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;
import java.text.SimpleDateFormat;
import java.util.*;

public class CreateRetall extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateRetall(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }
    /**
     * 发送并接收字符
     */
    public String getData(ArrayList<ArrayList> RETAIL,String totalPrice,String phone,String orderid) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        System.err.println(Data(RETAIL,totalPrice,phone,orderid));
        String str = RESULT(Data(RETAIL,totalPrice,phone,orderid), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
         if (!"0".equals(code)) {
            throw new Exception(message);
        }
        return new JSONArray(str).getJSONObject(0).getString("objectid");
    }

    private MultiValueMap Data(ArrayList<ArrayList> RETAIL, String totalPrice,String phone,String orderid) {

        HashMap params = new HashMap();
        HashMap masterobj = new HashMap(); //头表记录对象
        masterobj.put("table", "M_RETAIL");
        masterobj.put("ID", "-1");
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(now);
        masterobj.put("BILLDATE", dateString);
        masterobj.put("REFNO", orderid);
        masterobj.put("C_STORE_ID__NAME", "石家庄长安万达店");
        masterobj.put("C_VIP_ID__CARDNO",phone);
        masterobj.put("DESCRIPTION", "积分商城");

        HashMap detailobjs = new HashMap(); //明细表记录对象
        ArrayList list = new ArrayList();
        list.add(710);
        //list.add(2337);
        list.add(774);

        detailobjs.put("reftables", list);
//****************************************************************************
        ArrayList refobjs = new ArrayList();
        HashMap ref1 = new HashMap();
        ref1.put("table", "M_RETAILITEM");//零售单明细


        List addList1 = new ArrayList();
        for (ArrayList data : RETAIL) {
            HashMap map1 = new HashMap();//addlist内容
            map1.put("id", "-1");
            map1.put("M_PRODUCT_ID__NAME", data.get(0));

            map1.put("QTY", data.get(1));
            map1.put("PRICEACTUAL", data.get(2));
            map1.put("TYPE", "1");
            map1.put("DESCRIPTION", "积分商城");

            addList1.add(map1);
        }

        ref1.put("addList", String.valueOf(addList1));


        refobjs.add(ref1);
        //************************************************

        HashMap ref3 = new HashMap();
        ref3.put("table", "M_RETAILPAYITEM");//付款明细
        ArrayList addList3 = new ArrayList();
        HashMap map3 = new HashMap();
        map3.put("id", "-1");
        map3.put("C_CURRENCY_ID__DESCRIPTION", "人民币");
        map3.put("C_PAYWAY_ID__NAME", "现金");
        map3.put("BASE_PAYAMOUNT", totalPrice);
        map3.put("PAYAMOUNT", totalPrice);
        addList3.add(map3);

        ref3.put("addList", String.valueOf(addList3));

        refobjs.add(ref3);

        detailobjs.put("refobjs", refobjs);

        params.put("masterobj", masterobj);
        params.put("detailobjs", detailobjs);
        //params.put("submit", "true");//是否提交
        return DATA("ProcessOrder", params, account, password);
    }

    public static void main(String[] args) {
        CreateRetall g = new CreateRetall("39.100.45.8:55322", "nea@burgeon.com.cn", "123");
        ArrayList<ArrayList> RETAIL = new ArrayList();
        ArrayList list = new ArrayList();
        list.add("XMCDB019999"); //条码
        list.add("1");
        list.add("0");
        RETAIL.add(list);
        try {
            System.err.println(g.getData(RETAIL,"0","17732141036","1323"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
